//
//  NetProgressHudView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/1.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit
class NetProgressHudView: UIView {

    public enum NetProgressHudType : Int{
        case Black
        case White
    }
    let HudWidth = 130 * YXWidthScale
    let HudHeight = 130 * YXWidthScale
    var tray : UIVisualEffectView?
    private let gradient = CAGradientLayer.init()
    
    init(supView : UIView,type : NetProgressHudType) {
        let frame = supView.bounds
        super.init(frame: frame)
        let originX = frame.size.width / 2.0 - HudWidth / 2
        let originY = frame.size.height / 2.0 - HudHeight / 2
        switch type {
        case .Black:
            tray = UIVisualEffectView.init(effect: UIBlurEffect.init(style: UIBlurEffectStyle.dark))
        case .White:
            tray = UIVisualEffectView.init(effect: UIBlurEffect.init(style: UIBlurEffectStyle.light))
        }
        tray!.frame = CGRect.init(origin: CGPoint.init(x: originX, y: originY), size: CGSize.init(width: HudWidth, height: HudHeight))
        tray!.layer.cornerRadius = 18 * YXWidthScale
        tray!.layer.masksToBounds = true
        self.addSubview(tray!)
        supView.addSubview(self)
        draw()
    }
    
//开始加载
    func beginRotate() -> Void {
        let anim = CABasicAnimation.init(keyPath: "transform.rotation.z")
        anim.fromValue = NSNumber.init(value: 0)
        anim.toValue = NSNumber.init(value: Double.pi * 2)
        anim.duration = 1
        anim.autoreverses = false
        anim.fillMode = kCAFillModeForwards
        anim.repeatCount = 1000
        anim.isRemovedOnCompletion = false
        self.gradient.add(anim, forKey: "Loading")
    }
//停止加载
    func endRotate() -> Void {
        gradient.removeAllAnimations()
        self.removeFromSuperview()
    }
    
    private func draw()->Void{
        let rotateLayer = CAShapeLayer.init()
        rotateLayer.frame = tray!.bounds
        let path = UIBezierPath.init(arcCenter: CGPoint.init(x: tray!.bounds.size.width / 2, y: tray!.bounds.size.height / 2), radius: tray!.bounds.size.width / 2 - 3, startAngle: 0.0, endAngle: CGFloat(Double.pi * 3 / 4), clockwise: false)
        rotateLayer.path = path.cgPath
        rotateLayer.fillColor = UIColor.clear.cgColor
        rotateLayer.lineWidth = 5.0 * YXWidthScale
        rotateLayer.lineCap = kCALineCapRound
        rotateLayer.strokeColor = UIColor.white.cgColor
        tray!.layer.addSublayer(rotateLayer)
        gradient.frame = tray!.bounds
        gradient.colors = [UIColor.white.cgColor,UIColor.init(white: 1, alpha: 0)]
        gradient.startPoint = CGPoint.init(x: 0.5, y: 0)
        gradient.endPoint = CGPoint.init(x: 0, y: 0.5)
        tray!.layer.addSublayer(gradient)
        gradient.cornerRadius = 10 * YXWidthScale
        gradient.mask = rotateLayer
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
